package com.devin.service.Impl;

import com.devin.dao.MultiMapper;
import com.devin.dao.PermissionMapper;
import com.devin.dao.RoleMapper;
import com.devin.pojo.Role;
import com.devin.service.RoleService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("roleService")
@Transactional
public class RoleServiceImpl  implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MultiMapper multiMapper;

    @Override
    public List<Role> getRoles() {
        List<Role> roles = roleMapper.getRoles();
        return roles;
    }
    @Override
    public List<Role> getRolesAll(int page,int pageSize){
        PageHelper.startPage(page,pageSize);
        List<Role> roles = roleMapper.getRolesAll();
        return roles;
    }

    @Override
    public void addRole(Role role, Integer[] permissionIds) {
        roleMapper.addRole(role);
        /*采用批量添加role_permission*/
        multiMapper.addRoleAndPermissions(role.getId(),permissionIds);
    }

    @Override
    public void delRole(int roleId) {
        roleMapper.delRoleAndPermission(roleId);
        roleMapper.delRole(roleId);
    }

    @Override
    public Role getRoleById(int roleId) {
        Role role = roleMapper.getRoleById(roleId);
        return role;
    }

    @Override
    public void addPermissions(Integer roleId, Integer[] permissionsIds) {
//        for(Integer each:permissionsIds){
//            roleMapper.addRoleAndPermission(roleId,each);
//        }
        /*批量添加role_permissions*/
        multiMapper.addRoleAndPermissions(roleId,permissionsIds);
    }

    @Override
    public void delPermissionByRoleId(Integer roleId, Integer permissionId) {
        roleMapper.delPermissionByRoleId(roleId,permissionId);
    }

    @Override
    public List<Role> getRoles2Json(Integer userId) {
        List<Role> roles =roleMapper.getRoles2Json(userId);
        return roles;
    }

    @Override
    public void delRolesChecked(Integer[] ids) {
        multiMapper.delRolesAndPermissionsChecked(ids);
        multiMapper.delRolesChecked(ids);
    }
}
